package com.ruoyi.web.controller.prevention;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.dto.PreventionCheckTaskConfigRefreshDTO;
import com.ruoyi.system.domain.dto.PreventionCheckTaskConfigTwoDTO;
import com.ruoyi.system.domain.dto.PreventionHiddenDeptTreeListDTO;
import com.ruoyi.system.domain.vo.PreventionCheckTaskConfigExportVO;
import com.ruoyi.system.domain.vo.PreventionCheckTaskConfigVO;
import com.ruoyi.system.domain.vo.PreventionHiddenDeptTreeListVO;
import com.ruoyi.system.entity.PreventionCheckTaskConfig;
import com.ruoyi.system.mapper.PreventionCheckTaskConfigMapper;
import com.ruoyi.system.mapper.PreventionHiddenInfoRecordMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.PreventionCheckTaskConfigService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 双重预防-排查任务配置表(PreventionCheckTaskConfig)表控制层
 *
 * @since 2022-11-18 13:58:19
 */
@RestController
@RequestMapping("preventionCheckTaskConfig")
public class PreventionCheckTaskConfigController extends BaseController {

    @Resource
    private PreventionCheckTaskConfigService preventionCheckTaskConfigService;
    @Resource
    private PreventionCheckTaskConfigMapper preventionCheckTaskConfigMapper;
    @Autowired
    private ISysDeptService deptService;
    @Resource
    private PreventionHiddenInfoRecordMapper preventionHiddenInfoRecordMapper;


    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(PreventionCheckTaskConfigVO preventionCheckTaskConfigVO) {
        startPage();
        List<PreventionCheckTaskConfigVO> preventionCheckTaskConfigs = this.preventionCheckTaskConfigService.queryByPage(preventionCheckTaskConfigVO);
        if (!CollectionUtils.isEmpty(preventionCheckTaskConfigs)) {
            for (PreventionCheckTaskConfigVO preventionCheckTaskConfig : preventionCheckTaskConfigs) {
                String countHidden = preventionHiddenInfoRecordMapper.getcountHidden(preventionCheckTaskConfig.getId());
                preventionCheckTaskConfig.setCountHidden(countHidden);
            }
        }
        return getDataTable(preventionCheckTaskConfigs);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.preventionCheckTaskConfigService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param preventionCheckTaskConfig 实体
     * @return 新增结果
     */
    @PostMapping("/create")
    public AjaxResult add(@RequestBody PreventionCheckTaskConfig preventionCheckTaskConfig) {
        return AjaxResult.success(this.preventionCheckTaskConfigService.insert(preventionCheckTaskConfig));
    }

    /**
     * 编辑数据
     *
     * @param preventionCheckTaskConfig 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody PreventionCheckTaskConfig preventionCheckTaskConfig) {
        return AjaxResult.success(this.preventionCheckTaskConfigService.update(preventionCheckTaskConfig));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.preventionCheckTaskConfigService.deleteById(id));
    }

    /**
     * 配置
     *
     * @param preventionCheckTaskConfig 实体
     * @return 编辑结果
     */
    @PostMapping("/updateWhetherConfig")
    public AjaxResult updateWhetherConfig(@RequestBody PreventionCheckTaskConfig preventionCheckTaskConfig) {
        preventionCheckTaskConfig.setWhetherConfig(0);
        return AjaxResult.success(this.preventionCheckTaskConfigService.update(preventionCheckTaskConfig));
    }

    /**
     * 发布
     *
     * @param preventionCheckTaskConfig 实体
     * @return 编辑结果
     */
    @PostMapping("/updateWhetherRelease")
    public AjaxResult updateWhetherRelease(@RequestBody PreventionCheckTaskConfig preventionCheckTaskConfig) {
        PreventionCheckTaskConfig byId = preventionCheckTaskConfigService.queryById(preventionCheckTaskConfig.getId());
        preventionCheckTaskConfig.setWhetherRelease(byId.getWhetherRelease() == 0 ? 1 : 0);
        return AjaxResult.success(this.preventionCheckTaskConfigService.update(preventionCheckTaskConfig));
    }

    /**
     * 批量配置
     *
     * @param dto 实体
     * @return 编辑结果
     */
    @PostMapping("/batchUpdateWhetherConfig")
    public AjaxResult batchUpdateWhetherConfig(@RequestBody PreventionCheckTaskConfigTwoDTO dto) {
        int update = 0;
        List<Integer> list = new ArrayList<>(Arrays.asList(dto.getIds()));
        if (ObjectUtils.isNotEmpty(dto)) {
            if (dto.getType() == 0) {
                if (!CollectionUtils.isEmpty(list)) {
                    for (Integer id : list) {
                        PreventionCheckTaskConfig preventionCheckTaskConfig = this.preventionCheckTaskConfigService.queryById(id);
                        preventionCheckTaskConfig.setCheckUserIds("");
                        preventionCheckTaskConfig.setCheckUserNames("");
                        preventionCheckTaskConfig.setTaskStartTime("");
                        preventionCheckTaskConfig.setWhetherConfig(1);
                        update = preventionCheckTaskConfigMapper.updateTwo(preventionCheckTaskConfig);
                    }
                }
            } else if (dto.getType() == 1) {
                if (!CollectionUtils.isEmpty(list)) {
                    for (Integer id : list) {
                        PreventionCheckTaskConfig preventionCheckTaskConfig = this.preventionCheckTaskConfigService.queryById(id);
                        preventionCheckTaskConfig.setCheckUserIds(dto.getCheckUserIds());
                        preventionCheckTaskConfig.setCheckUserNames(dto.getCheckUserNames());
                        preventionCheckTaskConfig.setTaskStartTime(dto.getTaskStartTime());
                        preventionCheckTaskConfig.setWhetherConfig(0);
                        update = preventionCheckTaskConfigMapper.update(preventionCheckTaskConfig);
                    }
                }
            }
        }
        return AjaxResult.success(update);
    }

    /**
     * 批量发布
     *
     * @param dto 实体
     * @return 编辑结果
     */
    @PostMapping("/batchUdateWhetherRelease")
    public AjaxResult batchUdateWhetherRelease(@RequestBody PreventionCheckTaskConfigTwoDTO dto) {
        PreventionCheckTaskConfig update = new PreventionCheckTaskConfig();
        List<Integer> list = new ArrayList<>(Arrays.asList(dto.getIds()));
        if (ObjectUtils.isNotEmpty(dto)) {
            if (dto.getType() == 0) {
                if (!CollectionUtils.isEmpty(list)) {
                    for (Integer id : list) {
                        PreventionCheckTaskConfig preventionCheckTaskConfig = this.preventionCheckTaskConfigService.queryById(id);
                        preventionCheckTaskConfig.setWhetherRelease(1);
                        update = this.preventionCheckTaskConfigService.update(preventionCheckTaskConfig);
                    }
                }
            } else if (dto.getType() == 1) {
                if (!CollectionUtils.isEmpty(list)) {
                    for (Integer id : list) {
                        PreventionCheckTaskConfig preventionCheckTaskConfig = this.preventionCheckTaskConfigService.queryById(id);
                        preventionCheckTaskConfig.setWhetherRelease(0);
                        update = this.preventionCheckTaskConfigService.update(preventionCheckTaskConfig);
                    }
                }
            }
        }
        return AjaxResult.success(update);
    }


    /**
     * 隐患排查治理__查询部门树结构信息
     *
     * @param dept
     * @return
     */
    @GetMapping("/hiddenDeptTree")
    public AjaxResult hiddenDeptTree(SysDept dept) {
        return success(deptService.selectToDeptTreeList(dept));
    }


    /**
     * 隐患排查治理__查询部门树结构信息_联动列表
     *
     * @return 查询结果
     */
    @GetMapping("/getHiddenDeptTreeList")
    public TableDataInfo getHiddenDeptTreeList(PreventionHiddenDeptTreeListDTO dto) {
        startPage();
        List<PreventionHiddenDeptTreeListVO> preventionCheckTaskConfigs = this.preventionCheckTaskConfigService.getHiddenDeptTreeList(dto);
        return getDataTable(preventionCheckTaskConfigs);
    }


    /**
     * 刷新任务
     */
    @PostMapping("/updateRefresh")
    public AjaxResult updateRefresh(@RequestBody PreventionCheckTaskConfigRefreshDTO dto) {
        return AjaxResult.success(this.preventionCheckTaskConfigService.updateRefresh(dto));
    }

    @Log(title = "排查任务详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreventionCheckTaskConfigVO preventionCheckTaskConfigVO) {
        List<PreventionCheckTaskConfigExportVO> list = preventionCheckTaskConfigService.export(preventionCheckTaskConfigVO);
        ExcelUtil<PreventionCheckTaskConfigExportVO> util = new ExcelUtil<>(PreventionCheckTaskConfigExportVO.class);
        util.exportExcel(response, list, "排查任务详情");
    }

    @GetMapping("scheduledConfigOne")
    public void scheduledConfigOne() {
        preventionCheckTaskConfigService.scheduledConfigOne();
    }

}

